var table,$;
layui.use(['table','form'],function () {
    table = layui.table;
    form = layui.form;
    $ = layui.$;
    initAssoData();
    $("#search").click(function () {
        initAssoData();
    })
    table.on('toolbar(assoList)', function(obj){
        // var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'add':
                layer.open({
                    type:1,
                    title: "新增社团",
                    fixed:false,
                    resize :false,
                    shadeClose: true,
                    area: ['550px'],
                    content:$('#addAsso'),
                    end:function(){
                        cleanAsso();
                    }
                });
                break;
            case 'delete':
                layer.msg('删除');
                break;
            case 'update':
                layer.msg('编辑');
                break;
        };
    });
    form.on('submit(assoSubmit)', function(data){
        // TODO 校验
        console.log(data)
        formSubmit(data);
        return false;
    });
    table.on('tool(assoList)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            delAsso(data,data.id);
        }
    });
    table.on('edit(assoList)',function(obj){
        var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
        var editVO = {"id":data.id,"value":value}
        console.log(JSON.stringify(editVO))
        $.ajax({
            type: "POST",
            data:JSON.stringify(editVO),
            url: "/asso/updateUniqueId",
            headers	:{'Content-Type': 'application/json'},

            success: function (data) {
                if(data.code==1){
                    layer.alert("修改成功",function(){
                        layer.closeAll();
                        initAssoData()
                    });
                }else{
                    layer.alert(data.msg,function () {
                        layer.closeAll();
                        initAssoData()
                    });

                }
            },
            error: function () {
                layer.alert("操作请求错误，=-=",function(){
                    layer.closeAll();
                    cleanUser();
                    //加载load方法
                    initStuData()
                });
            }
        });
    })
})

function initAssoData() {
    var id = $('#id').val();
    var uniqueId = $('#uniqueId').val();
    if(id == ""){
        id = 0;
    }
    table.render({
        elem: '#assoList'
        , url: '/asso/getAsso'
        , method: 'get'
        , where: {
            id:id,
            uniqueId:uniqueId
        }
        , limit: 10
        , page: true
        ,toolbar: 'default'
        , cols: [[
            {type: 'numbers'}
            , {field: 'id', title: 'ID', width: 80, unresize: true, sort: true}
            , {field: 'schoolNo', title: '学校编号',templet:function (d) {
                    return '<span><a onclick=\"showSchool('+'\''+d.schoolNo+'\''+')\">'+d.schoolNo+'</a></span>'
                }}
            , {field: 'collegeNo', title: '所属学院',templet:function (d) {
                    return '<span><a onclick=\"showCollege('+'\''+d.collegeNo+'\''+')\">'+d.collegeNo+'</a></span>'
                }}
            , {field: 'name', title: '社团名称'}
            , {field: 'no', title: '社团编号'}
            , {field: 'password', title: '密码'}
            , {field: 'profile', title: '简介'}
            , {field: 'assoType', title: '社团类型'}
            , {field: 'uniqueId', title: '负责人唯一号', edit: 'text'}
            ,{fixed:'right', title:'操作',width:140,align:'center', toolbar:'#optBar'}
        ]]
    })
}
function formSubmit(data){
    console.log(JSON.stringify(data.field))
    $.ajax({
        type: "POST",
        data: JSON.stringify(data.field),
        url: "/asso/addAsso",
        headers	:{'Content-Type': 'application/json'},

        success: function (data) {
            if(data == "ok"){
                layer.alert("操作成功",function(){
                    layer.closeAll();
                    cleanUser();
                    initStuData()
                });
            }else{
                layer.alert(data);
            }
        },
        error: function () {
            layer.alert("操作请求错误，=-=",function(){
                layer.closeAll();
                cleanUser();
                //加载load方法
                initStuData()
            });
        }
    });
}
function showSchool(e) {
    layer.msg(e)
}
function showCollege(e) {
    console.log(e)
    layer.msg(e)
}
function cleanAsso(){
    //$("#id").val("");
    $("#password").val("123456");
    $("#name").val("");
    $("#no").val("");
    $("#schoolNo").val("");
    $("#collegeNo").val("");
    $("#profile").val("");
}
function delAsso(obj,id) {
    if(null!=id){
        layer.confirm('您确定要删除'+id+'社团吗？', {
            btn: ['确认','返回'] //按钮
        }, function(){
            $.post("/asso/delAsso",{"id":id},function(data){
                if(data.code==1){
                    //回调弹框
                    layer.alert("删除成功！",function(){
                        layer.closeAll();
                        initAssoData();
                    });
                }else{
                    layer.alert(data.msg,function(){
                        layer.closeAll();
                        initAssoData();

                    });
                }
            });}, function(){
            layer.closeAll();
        });
    }
}

